package co.edu.uniandes.distribuidos.fss.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

import co.edu.uniandes.distribuidos.fss.adv.FileAdvertisement;
import co.edu.uniandes.distribuidos.fss.adv.FileID;
import co.edu.uniandes.distribuidos.peer.constants.FileTypeConstants;

public class SharedFile {
	
	private String[] videoExt={"mpg","avi","mpeg"};
	
	private String[] audioExt={"mp3","wav","wma"};
	
	private String[] textExt={"pdf","doc","docx","txt","xlsx"};
	
	private FileAdvertisement adv;
	
	private File file;
	
	private String keywords;
	private String pipeID;
	private String peerName;
	private String peeID;
	
	public SharedFile(File file,String keywords, String pipeID,String peerName, String peeID) throws FileNotFoundException, IOException{
		this.file=file;
		this.keywords=keywords;
		this.pipeID = pipeID;
		this.peerName=peerName;
		this.peeID=peeID;
		this.adv=createAdvertisment();
	}
	
	private FileAdvertisement createAdvertisment() throws FileNotFoundException, IOException {
		FileAdvertisement advertisement=new FileAdvertisement();
		advertisement.setContentType(getContentType());
		advertisement.setFileID(new FileID(new FileInputStream(file),peeID));
		advertisement.setUrl(file.getAbsolutePath());
		advertisement.setKeywords(keywords);
		advertisement.setName(file.getName());
		advertisement.setSize(file.length());
		advertisement.setPipeID(pipeID);
		advertisement.setAddress(peerName);
		return advertisement;
	}

	private String getContentType() {
		int dot = file.getName().lastIndexOf(".");
		String extension=file.getName().substring(dot + 1);
		if(Arrays.asList(videoExt).contains(extension)){
			return FileTypeConstants.VIDEO;
		}
		if(Arrays.asList(audioExt).contains(extension)){
			return FileTypeConstants.AUDIO;
		}
		if(Arrays.asList(textExt).contains(extension)){
			return FileTypeConstants.TEXT;
		}
		return FileTypeConstants.NON_DETERMINATED;
	}

	public FileAdvertisement getAdvertisement(){
		return adv;
	}

	public FileAdvertisement getAdv() {
		return adv;
	}

	public void setAdv(FileAdvertisement adv) {
		this.adv = adv;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}


}
